package com.example.demo.controller;


import cn.hutool.core.io.FileUtil;
import com.example.demo.service.ImportExcelService;
import com.example.demo.utils.Result;
import com.example.demo.utils.SystemCodeEnum;
import com.example.demo.vo.UploadData;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import static org.apache.poi.ss.usermodel.CellType.STRING;
import static org.apache.poi.ss.usermodel.CellType.forInt;

/**
 * @Description Excel导入
 * @Author zhaoHeng
 * @Date 2021/12/31 0031 10:15
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/upload")
public class ImportExcelController {
    @Resource
    private ImportExcelService importExcelService;

    @PostMapping("/uploadExcel/test")
    @ResponseBody
    public Result uploadExcel2(@RequestParam("file") MultipartFile[] files) throws IOException {
        for (int i = 0; i < files.length; i++) {
            MultipartFile file = files[i];
            File fromStream = FileUtil.writeFromStream(file.getInputStream(), FileUtil.getTmpDirPath() + "/" + file.getOriginalFilename());
            POIFSFileSystem fs = new POIFSFileSystem(fromStream);  //接受一个输入流
            HSSFWorkbook wb = new HSSFWorkbook(fs);//创建了一个工作簿
            String filename = file.getOriginalFilename();
            if (!filename.endsWith(".xls")) {
                return Result.error(SystemCodeEnum.SYSTEM_UPLOAD_FILE_ERROR);
            }

            String tableName = filename.substring(0, filename.indexOf("."));
             importExcelService.upload(wb, tableName);
        }
        //File fromStream = FileUtil.writeFromStream(file.getInputStream(), FileUtil.getTmpDirPath() + "/" + file.getOriginalFilename());
        //POIFSFileSystem fs = new POIFSFileSystem(fromStream);  //接受一个输入流
        //HSSFWorkbook wb = new HSSFWorkbook(fs);//创建了一个工作簿
        //String filename = file.getOriginalFilename();
        //if (!filename.endsWith(".xls")) {
        //    return Result.error(SystemCodeEnum.SYSTEM_UPLOAD_FILE_ERROR);
        //}
        //
        //String tableName = filename.substring(0, filename.indexOf("."));
        //return importExcelService.upload(wb, tableName);

    return Result.ok(SystemCodeEnum.SYSTEM_OK);

    }


}
